package com.zx.pm.controller;

import com.zx.common.annotation.Anonymous;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.core.page.TableDataInfo;
import com.zx.pm.domain.Project;
import com.zx.pm.service.BidService;
import com.zx.pm.vo.ProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;
@Slf4j
@RestController
@RequestMapping("/pm/bid")
public class BidController extends BaseController {
    @Resource
    private BidService bidService;

    //查询所有的投标信息
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(Project project) {
      log.info("后端接收的参数: " + project);
        startPage();
        List<Project> list = bidService.selectBidList(project);
        return getDataTable(list);

    }

    //添加投标信息
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Project project) {
        if (bidService.checkUnitNameUnique(project)) {
            return AjaxResult.error("投标信息已存在" + project.getProjectName() + "'失败，投标信息名称已存在");
        } else if (bidService.checkUnitprojectNoUnique(project)) {
            return AjaxResult.error("投标编号" + project.getProjectNo() + "'失败，投标信息编号已存在");
        }
        //添加创建者
        project.setCreateBy(getUsername());
        project.setCreateTime(new Date());
        int n = bidService.insertBid(project);
        return AjaxResult.success(n);
    }

    // 修改投标信息
    @PostMapping("/update")
    @Anonymous
    public AjaxResult update(@Validated @RequestBody Project project) {
        // 检查项目名称是否唯一
        if (bidService.checkUnitNameUnique(project)) {
            return AjaxResult.error("投标信息'" + project.getProjectName() + "'失败，投标信息名称已存在");
        }
        // 检查项目编号是否唯一
        else if (bidService.checkUnitprojectNoUnique(project)) {
            return AjaxResult.error("投标编号'" + project.getProjectNo() + "'失败，投标信息编号已存在");
        }

        // 设置更新者和更新时间
        project.setUpdateBy(getUsername());
        project.setUpdateTime(new Date());
        // 执行更新操作
        int b = bidService.updateBid(project);
        return AjaxResult.success(b);
    }

    //存档
    @PostMapping("/archive")
    @Anonymous
    public AjaxResult archive(@Validated @RequestBody ProjectVo projectVo) {
        //校验逻辑放在方法最前面，越早失败越省资源，越清晰。
       try  {
            // 参数校验
            if (projectVo.getId() == null) {
                return AjaxResult.error("项目ID不能为空");
            }
            bidService.archiveProject(projectVo);
            return AjaxResult.success("项目存档成功");
        } catch (IllegalArgumentException e) {
            // 处理业务逻辑异常
            return AjaxResult.error("项目存档失败: " + e.getMessage());
        } catch (Exception e) {
            // 记录系统异常日志
            logger.error("项目存档失败", e);
            return AjaxResult.error("项目存档失败: 系统内部错误");
        }
    }



    //查询性别比列

    @GetMapping("/getSexRatio")
    @Anonymous
    public AjaxResult getSexRatio() {
        //根据性别查询数量
        int countMan = bidService.countBySex(0);
        int countWoman = bidService.countBySex(1);
        HashMap<String, List<Object>> map = new HashMap<>();
        map.put("sex", Arrays.asList("男", "女"));
        map.put("count", Arrays.asList(countMan, countWoman));
        return AjaxResult.success(map);

    }



    //查看报表
    @GetMapping("/report/{projectId}")
    public AjaxResult getReport(@PathVariable Long projectId) {
        Project  project = bidService.buildReport(projectId);
        return AjaxResult.success(project);
    }


        //字典
   @GetMapping("/dropdown")
   @PreAuthorize("@ss.hasPermi('pm:bid:query')")
   public AjaxResult dropdown() {
       return AjaxResult.success(bidService.selectEnabled());
   }






    @GetMapping("/getAll")
    public AjaxResult getAll(Project project){
        List<Project> list = bidService.selectBidList(project);
        return AjaxResult.success(list);
    }
}
